package info.nightbrest.agt.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.http.client.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import info.nightbrest.agt.shared.rpc.RpcParserException;

public interface AgtDispatcher
{
	public interface Action<R extends Result>
	{
		void init();
		void setRequestData(JavaScriptObject jso);
		void setRequestUri(String uri);
		void setResult(R result);

		String                getRequestUri();
		RequestBuilder.Method getRequestMethod();
		JavaScriptObject      getRequestData();
		String                getPostData(RpcDataFormat format) throws Exception;
		R                     getResult();

		public void onResponseReceived(Request request, Response response) throws RpcParserException, RpcParserException;
	}


	public interface Result
	{
		public interface Status
		{
			public final static String ACTION_STATUS_OK    = "ok";
			public final static String ACTION_STATUS_ERROR = "error";

			String getValue();
			String getMessage();
			boolean isOk();
		}

		Status getStatus();
		JavaScriptObject getData();
		void setStatus(Status status);
		void setData(JavaScriptObject data);
		boolean isOk();
	}

	public enum RpcDataFormat
	{
		DATA_JSON_FORMAT ("json", "application/json; charset=utf-8"),
		DATA_XML_FORMAT  ("xml",  "text/plain; charset=utf-8"),
		DATA_PLAIN_FORMAT("text", "text/plain");

		private final String name;
		private final String contentType;

		private RpcDataFormat(String name, String contentType) {
			this.contentType = contentType;
			this.name = name;
		}

		public String getContentType() {
			return contentType;
		}

		@Override
		public String toString() {
			return name;
		}
	}


	public <A extends Action<R>, R extends Result> void action(A action, AsyncCallback<R> callback);
	public <R extends Result> Result getLastResult(Class<R> clazz);
}
